package dao.impl;

import dao.BaseDao;
import dao.UserDao;
import entity.User;
import utils.Page;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImp extends BaseDao implements UserDao {

    public static void main(String[] args) {
        UserDaoImp userDaoImp = new UserDaoImp();
        //List<User> all = userDaoImp.getAll();
        //all.forEach(System.out::println);
        Integer i = userDaoImp.deleteById(3);
        System.out.println(i);
    }

    @Override
    public List<User> getAll() {
        String sql = "select id,username,birthday from kt_user";
        try {
            return executeQuery(sql, rs -> {
                List<User> list = new ArrayList<>();
                try {
                    while (rs.next()) {
                        User user = new User();
                        user.setId(rs.getInt("id"));
                        user.setUsername(rs.getString("username"));
                        user.setBirthday(rs.getDate("birthday"));
                        list.add(user);
                    }
                } catch (Exception e) {
                }
                return list;
            });
        } catch (Exception e) {
        }
        return null;
    }

    @Override
    public Integer deleteById(Integer id) {
        String sql = "delete from kt_user where id = ?";
        int count = 0;
        try {
            count = this.executeUpdate(sql, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public Integer add(User user) {
        String sql = "insert into kt_user(username,birthday,is_deleted,gender,phone) values(?,?,?,?,?)";
        try {
            return this.executeUpdate(sql, user.getUsername(), user.getBirthday(),
                    user.getIs_deleted(), user.getGender(), user.getPhone());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Integer updateById(User user) {
        String sql = "update kt_user set username = ?,birthday=?, gender= ?, phone = ?,is_deleted = ? where id =?";
        try {
            return this.executeUpdate(sql, user.getUsername(), user.getBirthday(),
                    user.getGender(), user.getPhone(),
                    user.getIs_deleted(), user.getId()
            );
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public User getById(Integer id) {
        String sql = "select id,username,birthday,is_deleted,gender,phone from kt_user where id = ?";
        try {
            return this.executeQuery(sql, rs -> {
                User user = new User();
                try {
                    while (rs.next()) {
                        user.setId(rs.getInt("id"));
                        user.setUsername(rs.getString("username"));
                        user.setBirthday(rs.getDate("birthday"));
                        user.setPhone(rs.getString("phone"));
                        user.setIs_deleted(rs.getInt("is_deleted"));
                        user.setGender(rs.getInt("gender"));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return user;
            }, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<User> getPage(Page page, User user) {
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();
        List<Object> list = new ArrayList<>();
        //String sql = "select * from kt_user  where 1=1 ORDER BY id asc  limit ?,? ";
        StringBuilder sql = new StringBuilder("select * from kt_user  where 1=1");
        if (user.getUsername() != null && user.getUsername().trim().length() > 0) {
            sql.append(" and username like concat('%',?,'%') ");
            list.add(user.getUsername());
        }
        list.add((pageNo - 1) * pageSize);
        list.add(pageSize);
        sql.append(" ORDER BY id asc  limit ?,?");
        try {
            return this.executeQuery(sql.toString(), rs -> {
                List<User> userList = new ArrayList<>();
                try {
                    while (rs.next()) {
                        User u = new User();
                        u.setId(rs.getInt("id"));
                        u.setUsername(rs.getString("username"));
                        u.setBirthday(rs.getDate("birthday"));
                        u.setPhone(rs.getString("phone"));
                        u.setIs_deleted(rs.getInt("is_deleted"));
                        u.setGender(rs.getInt("gender"));
                        userList.add(u);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return userList;
            }, list.toArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer count(User user) {
        StringBuilder sql = new StringBuilder("select count(1) from kt_user where 1 = 1 ");
        List<Object> list = new ArrayList<>();
        if (user.getUsername() != null && user.getUsername().trim().length() > 0) {
            sql.append(" and username like concat('%',?,'%')");
            list.add(user.getUsername());
        }
        try {
            return this.executeQuery(sql.toString(), rs -> {
                try {
                    rs.next();
                    return rs.getInt(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }, list.toArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
